package com.dr.dac.knowledgebase.ui.service;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast;

import com.dr.dac.knowledgebase.KBAidlInterface;
import com.dr.dac.knowledgebase.db.KClassDataBase;
import com.dr.dac.knowledgebase.db.KContentDataBase;
import com.dr.dac.knowledgebase.db.KFunctionDataBase;
import com.dr.dac.knowledgebase.ui.activity.MainActivity;
import com.dr.dac.knowledgebase.ui.bean.KClass;
import com.dr.dac.knowledgebase.ui.bean.KContent;
import com.dr.dac.knowledgebase.ui.bean.KFunction;
import com.dr.dac.knowledgebase.ui.utils.WindowViewManager;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by Nimo on 2016/12/26.
 */

public class AppServer extends Service {

    public static final String TAG = "AppServer";
    private ExecutorService cachedThreadPool;

    KClassDataBase kClassDataBase;
    KFunctionDataBase kFunctionDataBase;
    KContentDataBase kContentDataBase;

    @Override
    public void onCreate() {
        super.onCreate();
        Log.e(TAG, "onCreate() executed");
        initDB();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e(TAG, "onStartCommand() executed");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e(TAG, "onDestroy() executed");
    }

    @Override
    public IBinder onBind(Intent intent) {
        return new Binder();
    }

    public void initDB() {
        kClassDataBase = new KClassDataBase(this);
        kFunctionDataBase = new KFunctionDataBase(this);
        kContentDataBase = new KContentDataBase(this);
    }


    /**
     * cacheThreadPool 开启线程池
     */
    public void initThreadPool(final String HandleID) {
        if (null == cachedThreadPool) {
            cachedThreadPool = Executors.newCachedThreadPool();
        }
        cachedThreadPool.execute(new Runnable() {
            @Override
            public void run() {
                //查询关键字
                String KeyWord = kFunctionDataBase.queryKey(HandleID);
                List<KContent> list = kContentDataBase.query(" where KeyWord = " + "'" + KeyWord + "' ");
                if (null != list && list.size() > 0) {
                    showHintMessage();
                }
            }
        });
    }

    /**
     * @author Administrator
     *         使用类部类，返回当前service的实例，用于activity，调用service的各种方法
     */
    public class Binder extends KBAidlInterface.Stub {
        @Override
        public void show(String handleID) throws RemoteException {
            initThreadPool(handleID);
            //showHintMessage();
        }
    }

    /**
     * 展示提示信息
     */
    private  void showHintMessage(){
        handler.post(new Runnable() {
            @Override
            public void run() {
                WindowViewManager.createSmallWindow(getApplicationContext());
            }
        });
    }

    private Handler handler = new Handler();

}
